package com.new2i.util;

import java.util.HashMap;
import java.util.Iterator;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * JSON 转换为Collection工具类
 * @since 2015/08/27
 * @author 2i
 *
 */
public class Json2CollectionUtil {

	/**
	 * JSON字符串转换为HashMap 
	 * 仅为一层JSON的转换
	 * 
	 * @author 2i
	 * @since 2015/08/27
	 * @param jsonStr
	 * @return
	 */
	public static HashMap<String,Object> toHashMap(String jsonStr){
		HashMap<String, Object> sHashMap = new HashMap<String,Object>();
		try {
			JSONObject json = new JSONObject(jsonStr);
			Iterator<?> iter = json.keys();
			while(iter.hasNext()) {
				  String key = (String) iter.next();
				  Object value = json.get(key);
				  if(value == null)
					  value = ""; 
				  sHashMap.put(key, value);
			} 
		} catch (JSONException e) { 
			System.out.println(e.getLocalizedMessage()); 
		}
		return sHashMap; 
	}
}
